//
//  DetailViewController.m
//  代号：47
//
//  Created by 杨佩璋 on 15/7/5.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "DetailViewController.h"
#import "NSString+Html.h"

@interface DetailViewController ()<UIWebViewDelegate>
{
    MBProgressHUD *hud;
}
@property (nonatomic,retain)NSString *content;
@property (nonatomic,retain)AFHTTPRequestOperation *operation; // 操作。
@property (nonatomic,retain) NSOperationQueue *queue;  // 操作队列
@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTitle];
    self.view.backgroundColor = [UIColor whiteColor];
    [self  createdownLoadProgress];
    [self requestData];
}


// 创建头文字
- (void)createTitle{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreen_width - 120, 30)];
    label.text = _mytitle;
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    [label release];

}




// 加载进度条
- (void)createdownLoadProgress{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.color = [UIColor grayColor];
    [self.view addSubview:hud];
    hud.labelText = @"努力加载中..";
    //设置模式为环状进度提示框
    [hud show:YES];
    [hud release];
}


// 替换
-(void)requestData{
    NSString *str = [NSString stringWithFormat:@"http://api.m.mtime.cn/News/Detail.api?newsId=%@",self.newsId];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    self.operation = operation;
    [operation release];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        self.content = dic[@"content"];
        [self dosomething];
    } failure:nil];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    self.queue = queue;
    [queue release];
    
}




- (void)dosomething{
    UIWebView *web = [[UIWebView alloc] initWithFrame:self.view.bounds];
    NSString *newHtmlStr = [NSString importStyleWithHtmlString:self.content];
    [web loadHTMLString:newHtmlStr baseURL:[NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath]];
    web.delegate = self;
    
    [self.view addSubview:web];
    [web release];
   }


-(void)webViewDidStartLoad:(UIWebView *)webView{
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    if (hud) {
        [hud hide:YES];
        [hud removeFromSuperview];
    }
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSMutableString *str = [NSMutableString string];
    [str appendString:@"var arr = document.getElementsByTagName('img');"];
      NSString *str2 = [NSString stringWithFormat:@"for(var i=0;i<arr.length;i++){arr[i].marginLeft = 100;arr[i].width = %f;}",kScreen_width -15];
    [str appendString:str2];
    [webView stringByEvaluatingJavaScriptFromString:str];
   
}



-(void)dealloc{
    [_mytitle release];
    [_operation release];
    [_queue release];
    [_newsId release];
    [_content release];
    [super dealloc];
}


@end
